#include "FollowsHandle.h"

FollowsHandle::FollowsHandle(void)
{
}

FollowsHandle::~FollowsHandle(void)
{
}

std::vector<std::vector<INTEGER>> FollowsHandle::RetrieveTransitiveFollows(
	std::vector<INTEGER> previous_in, std::vector<INTEGER> following_in)
{
	std::sort(following_in.begin(), following_in.end());

	std::vector<std::vector<INTEGER>> result_vector;
	std::vector<INTEGER> previous_out;
	std::vector<INTEGER> following_out;

	STMT previous_stmt;
	previous_stmt.node_type = _NULL;
	unsigned int i;
	for (i = 0; i < previous_in.size(); i++)
	{
		previous_stmt.statement_number = previous_in[i];
		STMT_ARRAY follow_temp = PKB::follows.RetrieveTransitiveFollowing(previous_stmt);

		for(auto it1 = follow_temp.begin(); it1 != follow_temp.end(); it1++)
		{
			INTEGER stmt_num = it1->statement_number;

			if (std::binary_search(following_in.begin(), following_in.end(), stmt_num))
			{
				previous_out.push_back(previous_in[i]);
				following_out.push_back(stmt_num);
			}
		}
	}

	result_vector.push_back(previous_out);
	result_vector.push_back(following_out);
	
	return result_vector;
}

std::vector<std::vector<INTEGER>> FollowsHandle::RetrieveFollows(
	std::vector<INTEGER> previous_in, std::vector<INTEGER> following_in)
{
	std::sort(following_in.begin(), following_in.end());

	std::vector<std::vector<INTEGER>> result_vector;
	std::vector<INTEGER> previous_out;
	std::vector<INTEGER> following_out;

	STMT previous_stmt;
	previous_stmt.node_type = _NULL;
	unsigned int i;
	for (i = 0; i < previous_in.size(); i++)
	{
		previous_stmt.statement_number = previous_in[i];
		int follow_stmt_number = PKB::follows.GetFollowing(previous_stmt).statement_number;

		if (follow_stmt_number != -1)
		{
			if (std::binary_search(following_in.begin(), following_in.end(), follow_stmt_number))
			{
				previous_out.push_back(previous_in[i]);
				following_out.push_back(follow_stmt_number);
			}
		}
	}

	result_vector.push_back(previous_out);
	result_vector.push_back(following_out);
	
	return result_vector;
}